import { getCloudData } from '../../utils/handleCloud';
const utils = require('../../utils/util');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    classifyTitle: '精选菜品',
    checkAll: 0,
    classify: [
      {
        title: '营养粥',
        icon: '/static/icon/index/zhou.png',
        type: 1
      },
      {
        title: '家常小炒',
        icon: '/static/icon/index/chaocai.png',
        type: 2
      },
      {
        title: '面食主义',
        icon: '/static/icon/index/mianshi.png',
        type: 3
      },
      {
        title: '凉菜',
        icon: '/static/icon/index/shala.png',
        type: 4
      },
    ],
    menu: [],
    menu1: []
  },

  getMenuData() {
    const that = this;
    let { menu } = that.data;
    getCloudData('menu', { gather: 'menu' })
      .then(res => {
        menu = utils.reSortArr(res[0].list);
        that.setData({
          menu,
          menu1: menu,
          classifyTitle: "精选菜品",
          checkAll: 0,
        })
      })
  },

  handleTab(e) {
    const { type, title } = e.currentTarget.dataset;
    let { menu, menu1 } = this.data;
    menu = menu1.filter(i => i.type == type);
    this.setData({
      menu,
      classifyTitle: title,
      checkAll: type
    })
  },

  checkDetail(e) {
    const { title } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/index/detail/index?title=' + title,
    })
  },

  checkAll() {
    this.getMenuData();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMenuData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getMenuData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
